package com.lanou3g.bookstore.book.web.servlet.admin;

import com.lanou3g.bookstore.book.domain.Book;
import com.lanou3g.bookstore.book.service.BookService;
import com.lanou3g.bookstore.util.upload.UU;
import com.lanou3g.bookstore.util.upload.Upload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet(name = "AdminAddBookServlet", urlPatterns = "/adminjsps/admin/adminAddBook.do")
public class AdminAddBookServlet extends HttpServlet {
    private BookService bookService = new BookService();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Book book = new Book();
        Upload upload = new Upload(this);
        upload.parse(request);
        book.setBname(upload.getParameter("bname"))
                .setPrice(Double.valueOf(upload.getParameter("price")))
                .setAuthor(upload.getParameter("author"))
                .setCid(upload.getParameter("cid"))
                .setBid(UU.getUUID());
        String image = upload.getFileName("image");
        book.setImage("book_img/" + image);
        bookService.add(book);
        List<Book> books = bookService.findAll();
        request.setAttribute("books", books);
        request.getRequestDispatcher("/adminjsps/admin/book/list.jsp")
                .forward(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
